//
//  MainLogicLayer.cpp
//  plane_demo
//
//  Created by dummy on 13-6-22.
//
//

#include "MainLogicLayer.h"

#include "cocos2d.h"
#include "cocos-ext.h"
#include "../../GameMain.h"
#include "../PlanePoint.h"
#include "../PlaneUnit.h"
#include "../HeadQuarter.h"
#include "../BaseEvent.h"
#include "../../Compent/PopupDialog.h"
#include "../../Compent/EgCommon.h"

USING_NS_CC;
USING_NS_CC_EXT;


MainLogicLayer::MainLogicLayer()
{
    PlanePointMgr::NewInstance();
    PlaneUnitMgr::NewInstance();
}

MainLogicLayer::~MainLogicLayer()
{
    PlanePointMgr::ReleaseInstance();
    PlaneUnitMgr::ReleaseInstance();
    
    CCSpriteFrameCache::sharedSpriteFrameCache()->removeSpriteFramesFromFile("map_element.plist");
}

// on "init" you need to initialize your instance
void MainLogicLayer::onEnter()
{
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("map_element.plist", "map_element.png");
    CCSprite* wall = CCSprite::create("wallpaper.png");
    wall->setPosition(ccp(364, 262));
    m_CCLayer->addChild(wall);
    
    
    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();
        
    //this->schedule(schedule_selector(MainEditLayer::updateScene));
    CCDirector* pDirector = CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);
    
    m_labelTTF = CCLabelTTF::create("0", "微软雅黑", 100);
    m_labelTTF->setColor(ccc3(160, 20, 60));
    m_labelTTF->setPosition(ccp(size.width/2 - 150, size.height/2 - 220));
    m_CCLayer->addChild(m_labelTTF, 10);
    
    m_DiceL = CCLabelTTF::create("0", "微软雅黑", 100);
    m_DiceL->setColor(ccc3(160, 20, 60));
    m_DiceL->setPosition(ccp(size.width/2 - 150, size.height/2 - 50));
    m_CCLayer->addChild(m_DiceL, 10);
    
    m_DiceR = CCLabelTTF::create("0", "微软雅黑", 100);
    m_DiceR->setColor(ccc3(160, 20, 60));
    m_DiceR->setPosition(ccp(size.width/2 + 150, size.height/2 - 50));
    m_CCLayer->addChild(m_DiceR, 10);
    
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    CCPoint pointCenter = ccp(winSize.width / 2, winSize.height / 2);
    

    //        popupLayer();
    
    
    // 添加菜单
    CCMenu* menu = CCMenu::create();
    
    CCMenuItemFont* menuItem = CCMenuItemFont::create("popup", this, 0);
    menuItem->setPosition(ccp(200, 50));
    menuItem->setColor(ccc3(0, 0, 0));
    menu->addChild(menuItem);
    
    
    menu->setPosition(CCPointZero);
    m_CCLayer->addChild(menu);
    
    m_nowFrocesTurn = PLANE_FORCES_RED;
    m_headQuarters[PLANE_FORCES_BLUE] = new HeadQuarter(PLANE_FORCES_BLUE);
    m_headQuarters[PLANE_FORCES_BLUE]->addTolayer(m_CCLayer);
    m_headQuarters[PLANE_FORCES_YELLOW] = new HeadQuarter(PLANE_FORCES_YELLOW);
    m_headQuarters[PLANE_FORCES_YELLOW]->addTolayer(m_CCLayer);
    m_headQuarters[PLANE_FORCES_RED] = new HeadQuarter(PLANE_FORCES_RED);
    m_headQuarters[PLANE_FORCES_RED]->addTolayer(m_CCLayer);
    m_headQuarters[PLANE_FORCES_GREEN] = new HeadQuarter(PLANE_FORCES_GREEN);
    m_headQuarters[PLANE_FORCES_GREEN]->addTolayer(m_CCLayer);
    
//    std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("battleConfig.ini");
//    IniReader* ini = new IniReader();
//    ini->loadIniFile(fullPath.c_str());
//    int a = ini->getAttributeInt("stop_point", "stop");
    
    //init a default position
    m_CCLayer->setPosition(ccp(20, 210));
    
    GetPointMgr->init(m_CCLayer);
    GetUnitMgr->init(m_CCLayer);

    //ui
    m_uiLayer = UILayer::create();
    m_uiLayer->scheduleUpdate();

    UILayout* ui_root =dynamic_cast<UILayout*>(GUIReader::shareReader()->widgetFromJsonFile("plane_main.json"));
    m_uiLayer->addWidget(ui_root);

    UIButton* buttonFly = dynamic_cast<UIButton*>(m_uiLayer->getWidgetByName("button_fly"));
    buttonFly->addReleaseEvent(this, SEL_ReleaseEvent(&MainLogicLayer::btnFlyawayCB));

    UIButton* buttonBuild = dynamic_cast<UIButton*>(m_uiLayer->getWidgetByName("button_build"));
    buttonBuild->addReleaseEvent(this, SEL_ReleaseEvent(&MainLogicLayer::btnbuildCB));

    UILabelBMFont* forceTxt = dynamic_cast<UILabelBMFont*>(m_uiLayer->getWidgetByName("force_text"));
    forceTxt->setText("red");

    m_CCLayer->addChild(m_uiLayer);

    m_uiLayer->setPositionY(m_uiLayer->getPositionY() - 100);
    m_headQuarters[m_nowFrocesTurn]->turnStartRefresh();
    refreshNowForcesData();
}

void MainLogicLayer::btnbuildCB(CCObject* sender)
{
    m_headQuarters[m_nowFrocesTurn]->createPlane(PLANE_UNIT_NORMAL);
    refreshNowForcesData();
}

void MainLogicLayer::btnFlyawayCB(CCObject* sender)
{
    PlaneUnitMgr::PlaneUnits& units = PlaneUnitMgr::GetInstance()->getPlanes(m_nowFrocesTurn);
    if (units.size() == 0)
    {
        CCLOG("没飞机你飞个毛啊");
        return;
    }
    else
    {
        bool bHasHqPlane = false;
        for (PlaneUnitMgr::PlaneUnits::iterator i = units.begin(); i != units.end(); ++i)
        {
            if ((*i)->isInHq())
            {
                bHasHqPlane = true;
                break;
            }
        }
        
        if (bHasHqPlane)
        {
            int randNumber = GET_DICE;
            CCLOG("起飞骰子： %d", randNumber);
            if (randNumber == 6)
            {
                CCLOG("进入起飞状态！");
                m_headQuarters[m_nowFrocesTurn]->getUserData().actionState = PLANE_READY_STATE;
                return;
            }
            else
            {
                CCLOG("不是6，回合结束！");
                nextTurn();
                return;
            }
        }
        else
        {
            CCLOG("基地没飞机你飞个毛啊");
            return;
        }
    }
}

static char* PLANE_COLORS[PLANE_FORCES_COUNT] =
{
    "green",
    "red",
    "yellow",
    "blue"
};

void MainLogicLayer::nextTurn()
{
    m_nowFrocesTurn = (PLANE_FORCES) ((int)m_nowFrocesTurn + 1);
    if (m_nowFrocesTurn == PLANE_FORCES_COUNT)
    {
        m_nowFrocesTurn = (PLANE_FORCES) 0;
    }
    m_headQuarters[m_nowFrocesTurn]->turnStartRefresh();
    refreshNowForcesData();
}

void MainLogicLayer::refreshNowForcesData()
{
    UILabelBMFont* forceTxt = dynamic_cast<UILabelBMFont*>(m_uiLayer->getWidgetByName("force_text"));
    forceTxt->setText(PLANE_COLORS[m_nowFrocesTurn]);
    
    UILabelAtlas* goldTxt = dynamic_cast<UILabelAtlas*>(m_uiLayer->getWidgetByName("gold_text"));
    int nowForcesGold = m_headQuarters[m_nowFrocesTurn]->getUserData().gold;
    char num[10] = {0};
    sprintf(num, "%d", nowForcesGold);
    goldTxt->setStringValue(num);
}

void MainLogicLayer::printfDiceLR(const int& diceL_, const int& diceR_)
{
    char num[10] = {0};
    sprintf(num, "%d", diceL_);
    m_DiceL->setString(num);
    memset(num, 0, 10);
    sprintf(num, "%d", diceR_);
    m_DiceR->setString(num);
}

void MainLogicLayer::popCallback(cocos2d::CCNode *pNode)
{
    CCLog("button call back. tag: %d", pNode->getTag());
}

bool MainLogicLayer::ccTouchBegan(cocos2d::CCTouch *touch_, cocos2d::CCEvent *event_)
{
//    // 定义一个弹出层，传入一张背景图
//    PopupDialog* pl = PopupDialog::create("BackGround.png");
//   // pl->retain();
//    // ContentSize 是可选的设置，可以不设置，如果设置把它当作 9 图缩放
//    pl->setContentSize(CCSizeMake(400, 350));
//    pl->setTitle("title");
//    pl->setContentText("this is a dialog message box", 20, 60, 250);
//    // 设置回调函数，回调传回一个 CCNode 以获取 tag 判断点击的按钮
//    // 这只是作为一种封装实现，如果使用 delegate 那就能够更灵活的控制参数了
//    pl->setCallbackFunc(this, callfuncN_selector(MainLogicLayer::popCallback));
//    // 添加按钮，设置图片，文字，tag 信息
//    pl->addButton("pop_button.png", "pop_button.png", "OK", 0);
//    pl->addButton("pop_button.png", "pop_button.png", "CANCEL", 1);
//    // 添加到当前层
//    this->addChild(pl);
//    
//    return true;
    
    CCPoint relPoint = ccpSub(touch_->getLocation(), m_CCLayer->getPosition());
    
    BasePlaneUnit* unit = PlaneUnitMgr::GetInstance()->checkAndGetPlaneUnit(relPoint);
    if(unit && unit->getForces() == m_nowFrocesTurn)
    {
        if (m_headQuarters[m_nowFrocesTurn]->getUserData().actionState == PLANE_READY_STATE)
        {
            unit->isInHq()?unit->goForward(1):SGameMain->addFloatBar("please_select_hq");
        }
        else
        {
            if (unit->isInHq())
            {
                SGameMain->addFloatBar(".....");
            }
            else
            {
                int randNumber = 5;//GET_DICE;
                char num[10] = {0};
                sprintf(num, "%d", randNumber);
                m_labelTTF->setString(num);
                unit->goForward(randNumber);
                nextTurn();
            }
        }
        return false;
    }
    
    //for(int i = 0; i < PLANE_FORCES_COUNT; ++i)
    {
       // int i = m_nowFrocesTurn;
       // if(m_headQuarters[i]->onTouch(relPoint))
        //    return false;
    }
    //this->registerWithTouchDispatcher();
    return true;
}

void MainLogicLayer::ccTouchMoved(cocos2d::CCTouch *touch_, cocos2d::CCEvent *event_)
{
    CCPoint offset = touch_->getDelta();
    m_CCLayer->setPosition(ccpAdd(m_CCLayer->getPosition(), offset));
}

void MainLogicLayer::ccTouchEnded(cocos2d::CCTouch *touch_, cocos2d::CCEvent *event_)
{

}
